package com.blog.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.user.entity.UserAttention;
import com.blog.user.service.UserAttentionService;
import com.blog.utils.JWTUtils;
import com.blog.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zw
 * @since 2021-12-08
 */
@RestController
@RequestMapping("/userAttention")
@Slf4j
@CrossOrigin("*")
public class UserAttentionController {

    @Autowired
    UserAttentionService userAttentionService;

    //根据用户Id查询其关注列表
    @GetMapping("/getUserAttentionByUserId/{id}/{page}/{limit}")
    public ResponseResult getUserCollectionByUserId(@PathVariable String id, @PathVariable long page, @PathVariable long limit,
                                                    String username, @RequestHeader String token){
        if(!JWTUtils.checkToken(username, token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        Page<UserAttention> pageInfo=new Page<>(page,limit);
        userAttentionService.pageQuery(pageInfo,id);
        List<UserAttention> attentionList=pageInfo.getRecords();
        long total=pageInfo.getTotal();
        log.info(id+"获取了关注列表"+"第"+page+"页");
        return ResponseResult.ok().data("total",total).data("attentionList",attentionList);
    }
    //增加用户关注
    @PostMapping("/insertAttention")
    public ResponseResult insertCollection(String userId,String userAttentionId ,String username, @RequestHeader String token){
        userAttentionService.insertAttention(userId,userAttentionId);
        log.info(userId + "关注了"+userAttentionId);
        return ResponseResult.ok();
    }

    //取消用户关注
    @PostMapping("/deleteUserAttention")
    public ResponseResult deleteUserCollection(UserAttention userAttention,String username, @RequestHeader String token){
        if(!JWTUtils.checkToken(username, token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        userAttentionService.removeById(userAttention);
        log.info("关注表中"+userAttention.getId().toString()+"被删除了");
        return ResponseResult.ok();
    }

    //查询是否已经关注
    @GetMapping("/isAttented/{userId}/{userAttentionId}")
    public ResponseResult isAttented(@PathVariable  String userId,@PathVariable String userAttentionId,
                                      String username, @RequestHeader String token){
        if(!JWTUtils.checkToken(username, token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        boolean bool=userAttentionService.isAttented(userId,userAttentionId);
        log.info(userId+"是否关注了"+userAttentionId+":"+bool);
        return ResponseResult.ok().data("isAttention",bool);

    }
}

